//
//  OILoginViewController.m
//  EducationOID
//
//  Created by slz on 2017/5/11.
//  Copyright © 2017年 bnsr. All rights reserved.
//

#import "OILoginViewController.h"
#import "MFWUserLoginManager.h"
#import "OIRegisterViewController.h"
#import "YWStaticLoader.h"

@interface OILoginViewController ()
@property (weak, nonatomic) IBOutlet UITextField *uNameTextField;
@property (weak, nonatomic) IBOutlet UITextField *uPwdTextField;
@property (weak, nonatomic) IBOutlet UIButton *loginButton;
@property (weak, nonatomic) IBOutlet UIButton *registerButton;
@property (nonatomic,strong) UILabel *labe;

@end

@implementation OILoginViewController
{
    YWStaticLoader * loader;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setControllerTitle:@"登录"];
    self.showNavigationBar = YES;
    self.view.backgroundColor = kBack_Color_Default;
    UIBarButtonItem * item = [UIBarButtonItem itemWithImage:@"top_back_1" selectImage:nil addTarget:self andAction:@selector(backAction)];
    self.navigationItem.leftBarButtonItem = item;

    self.loginButton.layer.cornerRadius = 5;
    self.registerButton.layer.cornerRadius = 5;
    [self.view bringSubviewToFront:self.registerButton];
    
    // Do any additional setup after loading the view from its nib.
}

- (void)backAction{
    if (self.dismiss){
        self.dismiss(NO);
    }
}

- (IBAction)loginAction:(id)sender {
    BOOL shouldRequest = NO;
    NSString * errorInfoString = [self shouldSendLoginRequest:&shouldRequest];
    if (!shouldRequest){
        [self showErrorMsg:errorInfoString];
        return;
    }
    [self showProgress:@"正在登录..."];
    [[MFWUserLoginManager sharedInstance] loginWithOIDUserName:_uNameTextField.text password:_uPwdTextField.text finisheHandler:^(BOOL success, MFWUserInfo *userInfo, NSError *error) {
        [self hideProgress];
        if (success){
            [[NSUserDefaults standardUserDefaults] setObject:_uPwdTextField.text forKey:kUser_Info_pwd];
            [[NSUserDefaults standardUserDefaults] synchronize];
            [self.view showTip:@"登录成功" completion:^{
                if (self.dismiss){
                    self.dismiss(YES);
                }
            }];
        }else{
            [self showErrorMsg:[error.userInfo stringValueForKey:@"errorMsg"]];
        }
    }];
}

- (void)showErrorMsg:(NSString *)errorMsg{
    [self.view showTip:errorMsg];
}

- (NSString *)shouldSendLoginRequest:(BOOL *)shouldRequest{
    NSString * error;
    if ([MFWStringUtils isEmptyString:_uNameTextField.text] || [MFWStringUtils isEmptyString:_uPwdTextField.text]){
        *shouldRequest = NO;
        error = @"账号或密码不能为空";
    }else{
        *shouldRequest = YES;
    }
    return error;
}

- (IBAction)registerAction:(id)sender {
    OIRegisterViewController * registerController = [[OIRegisterViewController alloc]init];
    [self.navigationController pushViewController:registerController animated:YES];
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}

-(void)hideProgress
{
    if(loader) {
        [loader hidden];
        loader = nil;
    }
}

-(void)showProgress:(NSString*)msg{
    if (loader==nil){
        loader = [[YWStaticLoader alloc]initWithLoadingMsg:msg];
        [loader showInView:self.navigationController.view isRoot:NO];
    } else {
        [loader setLoadingMsg:msg];
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
